<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
            v-model="queryParams.title"
            placeholder="请输入标题"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['knowledge:classify:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['knowledge:classify:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-tree
        style="max-width: 600px"
        :data="classifyList"
        :props="defaultProps"
        node-key="id"
        accordion
        highlight-current
        draggable
    >
      <template v-slot:default="{ node }">
        <element-tree-line :node="node" :showLabelLine="false" :indent="16" @mouseover="node.showBtn = true" @mouseleave="node.showBtn = false" >
          <template v-slot:after-node-label>
                    <span style="padding: 0px 10px;display: flex;" v-show="node.showBtn">
                        <el-icon :size="15" @click="handleAdd(node)">
                          <CirclePlus />
                        </el-icon>
                        <el-icon :size="15" @click="handleUpdate(node)">
                          <Edit />
                        </el-icon>
                        <el-icon :size="15" @click="handleDelete(node)">
                          <Delete />
                        </el-icon>
                    </span>
          </template>
        </element-tree-line>
      </template>
    </el-tree>
    <!-- 添加或修改资源分类对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="classifyRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="租户ID" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入租户ID" />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="权限" prop="permissionStr">
          <el-input v-model="form.permissionStr" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="父ID" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入父ID" />
        </el-form-item>
        <el-form-item label="左值" prop="leftVal">
          <el-input v-model="form.leftVal" placeholder="请输入左值" />
        </el-form-item>
        <el-form-item label="右值" prop="rightVal">
          <el-input v-model="form.rightVal" placeholder="请输入右值" />
        </el-form-item>
        <el-form-item label="是否默认" prop="isDefault">
          <el-input v-model="form.isDefault" placeholder="请输入是否默认" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-input v-model="form.level" placeholder="请输入级别" />
        </el-form-item>
        <el-form-item label="创建人" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="最后更新人" prop="updatedBy">
          <el-input v-model="form.updatedBy" placeholder="请输入最后更新人" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Classify">
import { getClassify, delClassify, addClassify, updateClassify, classifyTree } from "@/api/knowledge/resourceClassify";
import { getElementLabelLine } from 'element-tree-line';
import {h} from "vue";

const ElementTreeLine = getElementLabelLine(h);
const { proxy } = getCurrentInstance();

const classifyList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const single = ref(true);
const multiple = ref(true);
const title = ref("");

const defaultProps = {
  children: 'children',
  label: 'title',
}

const data = reactive({
  form: {},
  queryParams: {
    title: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询资源分类列表 */
function getList() {
  loading.value = true;
  classifyTree(queryParams.value).then(response => {
    classifyList.value = response.data;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    tenantId: null,
    title: null,
    description: null,
    permissionStr: null,
    parentId: null,
    leftVal: null,
    rightVal: null,
    isDefault: null,
    sort: null,
    level: null,
    createTime: null,
    createdBy: null,
    updateTime: null,
    updatedBy: null
  };
  proxy.resetForm("classifyRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd(node) {
  reset();
  open.value = true;
  title.value = "添加资源分类";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || ids.value
  getClassify(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改资源分类";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["classifyRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateClassify(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addClassify(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除资源分类编号为"' + _ids + '"的数据项？').then(function() {
    return delClassify(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('knowledge/classify/export', {
    ...queryParams.value
  }, `classify_${new Date().getTime()}.xlsx`)
}

getList();
</script>
<style lang='scss' scoped>


</style>
